package cn.iocoder.yudao.module.promotion.controller.admin.yearcardorder;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.promotion.controller.admin.yearcardorder.vo.*;
import cn.iocoder.yudao.module.promotion.dal.dataobject.yearcardorder.YearCardOrderDO;
import cn.iocoder.yudao.module.promotion.service.yearcardorder.YearCardOrderService;

@Tag(name = "管理后台 - 年卡订单")
@RestController
@RequestMapping("/promotion/year-card-order")
@Validated
public class YearCardOrderController {

    @Resource
    private YearCardOrderService yearCardOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建年卡订单")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-order:create')")
    public CommonResult<Long> createYearCardOrder(@Valid @RequestBody YearCardOrderSaveReqVO createReqVO) {
        return success(yearCardOrderService.createYearCardOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新年卡订单")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-order:update')")
    public CommonResult<Boolean> updateYearCardOrder(@Valid @RequestBody YearCardOrderSaveReqVO updateReqVO) {
        yearCardOrderService.updateYearCardOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除年卡订单")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-order:delete')")
    public CommonResult<Boolean> deleteYearCardOrder(@RequestParam("id") Long id) {
        yearCardOrderService.deleteYearCardOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得年卡订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-order:query')")
    public CommonResult<YearCardOrderRespVO> getYearCardOrder(@RequestParam("id") Long id) {
        YearCardOrderDO yearCardOrder = yearCardOrderService.getYearCardOrder(id);
        return success(BeanUtils.toBean(yearCardOrder, YearCardOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得年卡订单分页")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-order:query')")
    public CommonResult<PageResult<YearCardOrderRespVO>> getYearCardOrderPage(@Valid YearCardOrderPageReqVO pageReqVO) {
        PageResult<YearCardOrderDO> pageResult = yearCardOrderService.getYearCardOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, YearCardOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出年卡订单 Excel")
//    @PreAuthorize("@ss.hasPermission('promotion:year-card-order:export')")
    @OperateLog(type = EXPORT)
    public void exportYearCardOrderExcel(@Valid YearCardOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<YearCardOrderDO> list = yearCardOrderService.getYearCardOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "年卡订单.xls", "数据", YearCardOrderRespVO.class,
                        BeanUtils.toBean(list, YearCardOrderRespVO.class));
    }

}